package vip.xiaomaoxiaoke.msg.message.impl;


import vip.xiaomaoxiaoke.msg.message.Message;
import vip.xiaomaoxiaoke.msg.repository.LocalMessageTable;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;



/**
 * 基于方法的本地消息内存
 * */
@Accessors(chain = true)
@Data
public class NativeMethodMessage  extends Message<NativeMethodMessage> {





    /**
     * 方法key
     * */
    private String methodKey;





    /**
     * 参数值列表
     * */
    private String paramValueList;

    @Override
    public LocalMessageTable convert() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("methodKey",methodKey);
        jsonObject.put("paramValueList",paramValueList);
        LocalMessageTable localMessageTable = new LocalMessageTable();
        localMessageTable.setOrderly(isOrderly());
        localMessageTable.setMsgId(getMsgId());
        localMessageTable.setMaxRetries(getMaxRetries());
        localMessageTable.setShardingKey(getShardingKey());
        localMessageTable.setBody(jsonObject.toJSONString());
        return localMessageTable;
    }

    @Override
    public NativeMethodMessage convert(LocalMessageTable localMessageTable) {
        JSONObject jsonObject = JSONObject.parseObject(localMessageTable.getBody());
        NativeMethodMessage nativeMethodMessage = new NativeMethodMessage();
        nativeMethodMessage.setMsgId(localMessageTable.getMsgId());
        nativeMethodMessage.setOrderly(localMessageTable.isOrderly());
        nativeMethodMessage.setShardingKey(localMessageTable.getShardingKey());
        nativeMethodMessage.setMethodKey(jsonObject.getString("methodKey"));
        nativeMethodMessage.setParamValueList(jsonObject.getString("paramValueList"));
        return nativeMethodMessage;
    }
}
